﻿using System;
using System.Web;

namespace HuoTu.Common
{
    /// <summary>
    /// cookies操作
    /// </summary>
    public class SysCookie
    {
        /// <summary>
        /// 设置cookies,包括值，做用域，有效路径，过期时间，字全级别（键值对）
        /// </summary>
        /// <param name="cookies">cookies集合名称</param>
        /// <param name="items">集合中元素名，用,分隔</param>
        /// <param name="values">集合中元素值，用,分隔</param>
        /// <param name="Expire">cookies过期时间(DateTime.Now.AddDays(1))</param>
        /// <param name="path">cookies的作用路径,一般不设置（不设置则为所有页）</param>
        /// <param name="domain">cookies指定有效的作用域，一般不指定</param>
        /// <param name="secure">指定cookies的安全级别，值为true或false，true为使用https:// 请求，一般不指定</param>
        public static void SetCookies(string cookies, string items, string values, DateTime Expire, string path, string domain, bool secure)
        {
            HttpCookie cookie = new HttpCookie(cookies);
            string[] item = items.Split(',');
            if (values != null)
            {
                string[] value = values.Split(',');
                for (Int32 i = 0; i < item.Length; i++)
                {
                    cookie[item[i].ToString()] = value[i].ToString();
                }
            }
            else
            {
                for (Int32 i = 0; i < item.Length; i++)
                {
                    cookie[item[i].ToString()] = null;
                }
            }
            if (path != null) cookie.Path = path;
            if (Expire.ToString() != null) cookie.Expires = Expire;
            if (domain != null) cookie.Domain = domain;
            if (secure.ToString() != null) cookie.Secure = secure;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 设置cookies,包括值，做用域，有效路径，字全级别（键值对）
        /// </summary>
        /// <param name="cookies">cookies集合名称</param>
        /// <param name="items">集合中元素名，用,分隔</param>
        /// <param name="values">集合中元素值，用,分隔</param>
        /// <param name="path">cookies的作用路径,一般不设置（不设置则为所有页）</param>
        /// <param name="domain">cookies指定有效的作用域，一般不指定</param>
        /// <param name="secure">指定cookies的安全级别，值为true或false，true为使用https:// 请求，一般不指定</param>
        public static void SetCookies(string cookies, string items, string values, string path, string domain, bool secure)
        {
            HttpCookie cookie = new HttpCookie(cookies);
            string[] item = items.Split(',');
            if (values != null)
            {
                string[] value = values.Split(',');
                for (Int32 i = 0; i < item.Length; i++)
                {
                    cookie[item[i].ToString()] = value[i].ToString();
                }
            }
            else
            {
                for (Int32 i = 0; i < item.Length; i++)
                {
                    cookie[item[i].ToString()] = null;
                }
            }
            if (path != null) cookie.Path = path;
            if (domain != null) cookie.Domain = domain;
            if (secure.ToString() != null) cookie.Secure = secure;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 设置cookies,包括值，做用域，有效路径（单值）
        /// </summary>
        /// <param name="cookieName">cookies集合名称</param>
        /// <param name="values">值</param>
        /// <param name="expire">cookies过期时间(DateTime.Now.AddDays(1))</param>
        /// <param name="path">cookies的作用路径,一般不设置（不设置则为所有页）</param>
        /// <param name="domain">cookies指定有效的作用域，一般不指定</param>
        public static void SetCookies(string cookieName, string value, DateTime expire, string path, string domain)
        {
            HttpCookie cookie = new HttpCookie(cookieName);
            if (cookie == null)
                cookie = new HttpCookie(cookieName, value);
            else
                cookie.Value = value;
            if (path != null) cookie.Path = path;
            if (expire.ToString() != null) cookie.Expires = expire;
            if (domain != null) cookie.Domain = domain;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 设置cookies,包括值，做用域，有效路径（单值）
        /// </summary>
        /// <param name="cookieName">cookies集合名称</param>
        /// <param name="values">值</param>
        /// <param name="expire">cookies过期时间(DateTime.Now.AddDays(1))</param>
        /// <param name="path">cookies的作用路径,一般不设置（不设置则为所有页）</param>
        /// <param name="domain">cookies指定有效的作用域，一般不指定</param>
        public static void SetCookies(string cookieName, string value, string path, string domain)
        {
            HttpCookie cookie = new HttpCookie(cookieName);
            if (cookie == null)
                cookie = new HttpCookie(cookieName, value);
            else
                cookie.Value = value;
            if (path != null) cookie.Path = path;
            if (domain != null) cookie.Domain = domain;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        /// <summary>
        /// 获得指定键的cookie值
        /// </summary>
        /// <param name="cookieName">cooike名称</param>
        /// <param name="cookieKey">键</param>
        public static string GetCookie(string cookieName, string cookieKey)
        {
            if (HttpContext.Current.Request.Cookies[cookieName] != null && HttpContext.Current.Request.Cookies[cookieName][cookieKey] != null)
            {
                return HttpContext.Current.Request.Cookies[cookieName][cookieKey].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 获取cookie值
        /// </summary>
        /// <param name="cookieName">cooike名称</param>
        /// <returns></returns>
        public static string GetCookie(string cookieName)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie != null)
                return cookie.Value;
            return string.Empty;
        }

        /// <summary>
        /// 检测Cookies的有效性
        /// </summary>
        /// <param name="cookiesName">要检测的Cookies名称</param>
        /// <param name="keyName">Cookies的键(为null 则为（单值）直接判断keyValue)</param>
        /// <param name="keyValue">值</param>
        /// <returns>是/否</returns>
        public static bool CheckCookies(string cookiesName, string keyName, string keyValue)
        {
            if (keyName != null)
            {
                if (HttpContext.Current.Request.Cookies[cookiesName] != null)
                {
                    if (HttpContext.Current.Request.Cookies[cookiesName][keyName] != string.Empty)
                    {
                        #region "是否需要检测键值"
                        if (keyValue != null)
                        {
                            //检测键值是否匹配
                            if (HttpContext.Current.Request.Cookies[cookiesName][keyName] == keyValue)
                            {
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        }
                        else
                        {
                            return true;
                        }
                        #endregion
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                if (HttpContext.Current.Request.Cookies[cookiesName] != null)
                {
                    if (HttpContext.Current.Request.Cookies[cookiesName].Value != string.Empty)
                    {
                        #region "是否需要检测Cookies值"
                        if (keyValue != null)
                        {
                            //检测Cookies值是否有效
                            if (HttpContext.Current.Request.Cookies[cookiesName].Value == keyValue)
                            {
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        }
                        else
                        {
                            return true;
                        }
                        #endregion
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
